home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / grep / grep.lzh / grep.c < prev    next >
C/C++ Source or Header  |  1989-03-02  |  28KB  |  1,048 lines

  1. /* grep - print lines matching an extended regular expression
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.                       Written June, 1988 by Mike Haertel
  4.                   BMG speedups added July, 1988
  5.             by James A. Woods and Arthur David Olson
  6.  
  7.                NO WARRANTY
  8.  
  9.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  10. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  11. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  12. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  13. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  14. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  15. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  16. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  17. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  18. CORRECTION.
  19.  
  20.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  21. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  22. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  23. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  24. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  25. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  26. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  27. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  28. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  29. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  30.  
  31.         GENERAL PUBLIC LICENSE TO COPY
  32.  
  33.   1. You may copy and distribute verbatim copies of this source file
  34. as you receive it, in any medium, provided that you conspicuously and
  35. appropriately publish on each copy a valid copyright notice "Copyright
  36.  (C) 1988 Free Software Foundation, Inc."; and include following the
  37. copyright notice a verbatim copy of the above disclaimer of warranty
  38. and of this License.  You may charge a distribution fee for the
  39. physical act of transferring a copy.
  40.  
  41.   2. You may modify your copy or copies of this source file or
  42. any portion of it, and copy and distribute such modifications under
  43. the terms of Paragraph 1 above, provided that you also do the following:
  44.  
  45.     a) cause the modified files to carry prominent notices stating
  46.     that you changed the files and the date of any change; and
  47.  
  48.     b) cause the whole of any work that you distribute or publish,
  49.     that in whole or in part contains or is a derivative of this
  50.     program or any part thereof, to be licensed at no charge to all
  51.     third parties on terms identical to those contained in this
  52.     License Agreement (except that you may choose to grant more extensive
  53.     warranty protection to some or all third parties, at your option).
  54.  
  55.     c) You may charge a distribution fee for the physical act of
  56.     transferring a copy, and you may at your option offer warranty
  57.     protection in exchange for a fee.
  58.  
  59. Mere aggregation of another unrelated program with this program (or its
  60. derivative) on a volume of a storage or distribution medium does not bring
  61. the other program under the scope of these terms.
  62.  
  63.   3. You may copy and distribute this program or any portion of it in
  64. compiled, executable or object code form under the terms of Paragraphs
  65. 1 and 2 above provided that you do the following:
  66.  
  67.     a) accompany it with the complete corresponding machine-readable
  68.     source code, which must be distributed under the terms of
  69.     Paragraphs 1 and 2 above; or,
  70.  
  71.     b) accompany it with a written offer, valid for at least three
  72.     years, to give any third party free (except for a nominal
  73.     shipping charge) a complete machine-readable copy of the
  74.     corresponding source code, to be distributed under the terms of
  75.     Paragraphs 1 and 2 above; or,
  76.  
  77.     c) accompany it with the information you received as to where the
  78.     corresponding source code may be obtained.  (This alternative is
  79.     allowed only for noncommercial distribution and only if you
  80.     received the program in object code or executable form alone.)
  81.  
  82. For an executable file, complete source code means all the source code for
  83. all modules it contains; but, as a special exception, it need not include
  84. source code for modules which are standard libraries that accompany the
  85. operating system on which the executable file runs.
  86.  
  87.   4. You may not copy, sublicense, distribute or transfer this program
  88. except as expressly provided under this License Agreement.  Any attempt
  89. otherwise to copy, sublicense, distribute or transfer this program is void and
  90. your rights to use the program under this License agreement shall be
  91. automatically terminated.  However, parties who have received computer
  92. software programs from you with this License Agreement will not have
  93. their licenses terminated so long as such parties remain in full compliance.
  94.  
  95.   5. If you wish to incorporate parts of this program into other free
  96. programs whose distribution conditions are different, write to the Free
  97. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  98. worked out a simple rule that can be stated here, but we will often permit
  99. this.  We will be guided by the two goals of preserving the free status of
  100. all derivatives our free software and of promoting the sharing and reuse of
  101. software.
  102.  
  103.  
  104. In other words, you are welcome to use, share and improve this program.
  105. You are forbidden to forbid anyone else to use, share and improve
  106. what you give them.   Help stamp out software-hoarding!  */
  107.  
  108. #include <ctype.h>
  109. #include <stdio.h>
  110. #ifdef USG
  111. #include <memory.h>
  112. #include <string.h>
  113. #else
  114. #include <strings.h>
  115. #endif
  116. #include "dfa.h"
  117. #include "regex.h"
  118.  
  119. #ifdef __STDC__
  120. extern getopt(int, char **, const char *);
  121. extern read(int, void *, int);
  122. extern open(const char *, int, ...);
  123. extern void close();
  124. #else
  125. extern char *strrchr();
  126. #endif
  127.  
  128. extern char *optarg;
  129. extern optind, opterr;
  130. extern errno;
  131. extern char *sys_errlist[];
  132.  
  133. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  134.  
  135. /* Exit status codes. */
  136. #define MATCHES_FOUND 0        /* Exit 0 if no errors and matches found. */
  137. #define NO_MATCHES_FOUND 1    /* Exit 1 if no matches were found. */
  138. #define ERROR 2            /* Exit 2 if some error occurred. */
  139.  
  140. /* Error is set true if something awful happened. */
  141. static int error;
  142.  
  143. /* The program name for error messages. */
  144. static char *prog;
  145.  
  146. /* We do all our own buffering by hand for efficiency. */
  147. static char *buffer;        /* The buffer itself, grown as needed. */
  148. static bufbytes;        /* Number of bytes in the buffer. */
  149. static size_t bufalloc;        /* Number of bytes allocated to the buffer. */
  150. static bufprev;            /* Number of bytes that have been forgotten.
  151.                    This is used to get byte offsets from the
  152.                    beginning of the file. */
  153. static bufread;            /* Number of bytes to get with each read(). */
  154.  
  155. static void
  156. initialize_buffer()
  157. {
  158.   bufread = 8192;
  159.   bufalloc = bufread + bufread / 2;
  160.   buffer = malloc(bufalloc);
  161.   if (! buffer)
  162.     {
  163.       fprintf(stderr, "%s: Memory exhausted (%s)\n", prog,
  164.           sys_errlist[errno]);
  165.       exit(ERROR);
  166.     }
  167. }
  168.  
  169. /* The current input file. */
  170. static fd;
  171. static char *filename;
  172. static eof;
  173.  
  174. /* Fill the buffer retaining the last n bytes at the beginning of the
  175.    newly filled buffer (for backward context).  Returns the number of new
  176.    bytes read from disk. */
  177. static
  178. fill_buffer_retaining(n)
  179.      int n;
  180. {
  181.   char *p, *q;
  182.   int i;
  183.  
  184.   /* See if we need to grow the buffer. */
  185.   if (bufalloc - n <= bufread)
  186.     {
  187.       while (bufalloc - n <= bufread)
  188.     {
  189.       bufalloc *= 2;
  190.       bufread *= 2;
  191.     }
  192.       buffer = realloc(buffer, bufalloc);
  193.       if (! buffer)
  194.     {
  195.       fprintf(stderr, "%s: Memory exhausted (%s)\n", prog,
  196.           sys_errlist[errno]);
  197.       exit(ERROR);
  198.     }
  199.     }
  200.  
  201.   bufprev += bufbytes - n;
  202.  
  203.   /* Shift stuff down. */
  204.   for (i = n, p = buffer, q = p + bufbytes - n; i--; )
  205.     *p++ = *q++;
  206.   bufbytes = n;
  207.  
  208.   if (eof)
  209.     return 0;
  210.  
  211.   /* Read in new stuff. */
  212.   i = read(fd, buffer + bufbytes, bufread);
  213.   if (i < 0)
  214.     {
  215.       fprintf(stderr, "%s: read on %s failed (%s)\n", prog,
  216.           filename ? filename : "<stdin>", sys_errlist[errno]);
  217.       error = 1;
  218.     }
  219.  
  220.   /* Kludge to pretend every nonempty file ends with a n